package com.snk.controller;

import com.snk.common.pojo.JsonResult;
import com.snk.common.utils.ObjectMapperUtil;
import com.snk.pojo.User;
import com.snk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user")
public class UserController {
    private static final String SNK_TICKET="SNK_TICKET";
    @Autowired
    private UserService userService;
    /**
     * 登录业务实现
     * url:http://www.nb.com/user/doLogin
     * */
    @ResponseBody
    @RequestMapping("/doLogin")
    public JsonResult doLogin(String username,String password, HttpServletResponse response){
        System.out.println(username+"=="+password);
        String userIdJson=null;
        if (StringUtils.hasLength(username)&&StringUtils.hasLength(password)){
            userIdJson =userService.doLogin(username,password);}
        if (StringUtils.hasLength(userIdJson)){
            Cookie cookie = new Cookie(SNK_TICKET,userIdJson);
            System.out.println("向Cookie中添加用户IDJson");
            cookie.setMaxAge(-1);
            cookie.setDomain("nb.com");
            cookie.setPath("/");
            response.addCookie(cookie);
            return new JsonResult("登录正确");
        }
        JsonResult jsonResult=new JsonResult();
        jsonResult.setState(0);
        jsonResult.setMessage("账户或密码错误");
        jsonResult.setData("失败");
        return jsonResult;
    }
}
